package com.ds.bpm.graph;


import com.ds.bpm.engine.BPMException;
import com.ds.bpm.engine.inter.EIActivityDef;
import com.ds.bpm.engine.inter.EIProcessDefVersion;
import com.ds.bpm.engine.inter.EIProcessDefVersionManager;
import com.ds.bpm.engine.inter.EIRouteDef;
import com.ds.common.util.StringUtility;
import com.ds.esd.bpm.node.EndNode;
import com.ds.esd.bpm.node.NoActiviteNode;
import com.ds.esd.bpm.node.RouteNode;
import com.ds.esd.bpm.node.StartNode;
import com.ds.esd.bpm.view.Graph;
import com.ds.esd.tool.ui.component.svg.SVGBaseComponent;

import java.util.List;

public class GraphBuilder {
    private EIProcessDefVersion eiProcessDefVersion;
    EIProcessDefVersionManager eiProcessDefVersionManager = EIProcessDefVersionManager.getInstance();

    public GraphBuilder(String processDevVersionId) throws BPMException {
        this.eiProcessDefVersion = eiProcessDefVersionManager.loadByKey(processDevVersionId);

    }

    /**
     * 生成流程定义图
     *
     * @return
     * @throws BPMException
     */
    public Graph build() throws BPMException {
        List<EIActivityDef> activityDefList = eiProcessDefVersion
                .getAllActivityDefs();
        Graph g = new Graph();
        List<EIRouteDef> routeDefList = eiProcessDefVersion.getAllRouteDefs();

        this.genActivityDefVertex(g, activityDefList);
        this.genEndVertex(g);
        this.genStartVertex(g);
        this.genRouteDefEdge(g, routeDefList);

        return g;
    }

    /**
     * 获取开始节点
     *
     * @param g
     * @return
     * @throws BPMException
     */
    private Graph genStartVertex(Graph g)
            throws BPMException {

        String[] startStrings = StringUtility.split(
                eiProcessDefVersion.getAttributeInterpretedValue(
                        "BPD.StartOfWorkflow").toString(), ";");
        //  String startId = (UUID.randomUUID().toString()).toString();
        String startActivityDefId = startStrings[1];
        int satrtX = Integer.valueOf(startStrings[2]);
        int satrtY = Integer.valueOf(startStrings[3]);
        StartNode starNode = new StartNode(satrtX, satrtY, "start");

        g.addChileNode(starNode);

        RouteNode routeNode = new RouteNode(g.getNode(starNode.getAlias()), g.getNode(startActivityDefId), "startActivityDefId", "开始");

        g.addChileNode(routeNode);
        return g;
    }

    /**
     * 获取结束节点
     *
     * @param g
     * @return
     * @throws BPMException
     */
    private Graph genEndVertex(Graph g)
            throws BPMException {

        if (eiProcessDefVersion
                .getAttributeInterpretedValue("BPD.EndOfWorkflow") != null) {
            String[] ends = StringUtility.split(eiProcessDefVersion
                    .getAttributeInterpretedValue("BPD.EndOfWorkflow")
                    .toString(), "$");
            for (int k = 0; k < ends.length; k++) {
                String[] endStrings = StringUtility.split(ends[k], ";");
                //  String endId = (UUID.randomUUID().toString()).toString();
                String endActivityDefId = endStrings[1];
                int endX = Integer.valueOf(endStrings[2]);
                int endY = Integer.valueOf(endStrings[3]);
                EndNode endNode = new EndNode(endX, endY, endActivityDefId + "LAST");
                g.addChileNode(endNode);

            }
        }

        return g;

    }

    /**
     * 添加活动定义节点
     *
     * @param g
     * @return
     * @throws BPMException
     */
    private Graph genActivityDefVertex(Graph g,
                                       List<EIActivityDef> activityDefList) throws BPMException {
        for (int k = 0; k < activityDefList.size(); k++) {
            EIActivityDef eiActivityDef = activityDefList.get(k);

            String text = eiActivityDef.getName();
            int x = Integer.valueOf((String) eiActivityDef
                    .getAttributeInterpretedValue("BPD.XOffset"));
            int y = Integer.valueOf((String) eiActivityDef
                    .getAttributeInterpretedValue("BPD.YOffset"));
            NoActiviteNode v = new NoActiviteNode(x, y, eiActivityDef.getActivityDefId(), text, text);

            g.addChileNode(v);

        }
        return g;
    }

    /**
     * 添加路由定义
     *
     * @param g
     * @return
     * @throws BPMException
     */
    private Graph genRouteDefEdge(Graph g, List<EIRouteDef> routeDefList)
            throws BPMException {
        for (int j = 0; j < routeDefList.size(); j++) {
            EIRouteDef routeDef = routeDefList.get(j);
            String routeId = routeDef.getRouteDefId();
            String endId = routeDef.getToActivityDefId();
            if (routeDef.getFromActivityDef()!=null){
                String startId = routeDef.getFromActivityDef().getActivityDefId();
                if (endId.equals("LAST")) {
                    endId = startId + "LAST";
                }
                SVGBaseComponent start = g.getNode(startId);
                SVGBaseComponent end = g.getNode(endId);
                String title = routeDef.getName().length() > 8 ? routeDef.getName().substring(0, 8) + "..." : routeDef.getName();
                if (start != null && end != null) {
                    RouteNode routeNode = new RouteNode(start, end, routeId, title);
                    g.addChileNode(routeNode);
                }
            }

        }

        return g;
    }

}
